import {CaseType} from "../enum";

/**
 * 是否为字符串类型
 *
 * @param {*} value
 * @return {boolean}
 */
const isString = (value: any) => {
    return typeof value === 'string' || value instanceof String;
}

/**
 *  字母大小写切换的工具方法
 *
 * @param {string} str
 * @return {string}
 */
const toggleCase = (str: string): string => {
    let itemText = "";
    str.split("").forEach((item) => {
        if (/^([a-z]+)/.test(item)) {
            itemText += item.toUpperCase();
        } else if (/^([A-Z]+)/.test(item)) {
            itemText += item.toLowerCase();
        } else {
            itemText += item;
        }
    });
    return itemText;
}

/**
 *  字母大小写切换
 *
 * @param {string} source
 * @param {number} caseType
 * @return {string}
 * @example
 *
 * changeCase('hello world',1)
 * // => Hello World
 *
 * changeCase('Hello World',2)
 * // => hello world
 *
 * changeCase('HI,hello world',3)
 * // => hi,HELLO WORLD
 *
 * changeCase('hello world',4)
 * // => HELLO WORLD
 *
 * changeCase('HELLO WORLD',5)
 * // => hello world
 */
const changeCase = (source: string, caseType: number): string => {

    switch (caseType) {
        case CaseType.uppercaseFirst:
            return source.replace(/\b\w+\b/g, (word) => {
                return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
            });
        case CaseType.lowercaseFirst:
            return source.replace(/\b\w+\b/g, (word) => {
                return word.substring(0, 1).toLowerCase() + word.substring(1);
            });
        case CaseType.toggleCase:
            return toggleCase(source);
        case CaseType.uppercaseAll:
            return source.toUpperCase();
        case CaseType.lowercaseAll:
            return source.toLowerCase();
        default:
            return source
    }
}

/**
 * 生成uuid
 *
 * @param {number} len
 * @param {number} radix
 * @return {string}
 */
const genUUID = (len: number, radix: number): string => {
    let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    let uuid = [];
    let i;
    radix = radix || chars.length;
    if (len) {
        // Compact form
        for (i = 0; i < len; i++) {
            uuid[i] = chars[0 | (Math.random() * radix)];
        }
    } else {
        // rfc4122, version 4 form
        let r;

        // rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';

        // Fill in random data.  At i==19 set the high bits of clock sequence as per
        // rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | (Math.random() * 16);
                uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return changeCase(uuid.join(''), 5)
}

/**
 *  转字符串
 *
 * @param {any} source
 * @return {string}
 * @example
 *
 * toString({name:'demo'})
 *
 * {
 *   "name": "demo"
 * }
 */
const toString = (source: any): string => {
    return source == null ? '' : typeof source === 'object' ? JSON.stringify(source, null, 2) : String(source);
}

/**
 *  转小驼峰写法
 *
 * @param {string} str
 * @return {string}
 * @example
 *
 * toCamelCase('hello-world')
 * // => helloWorld
 */
const toCamelCase = (str: string): string => {
    if (!str) {
        return '';
    }
    // @ts-ignore
    const s = str
        .toLowerCase()
        .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
        .map((x: string) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
        .join('')
    return s.slice(0, 1).toLowerCase() + s.slice(1)
}

export {
    toString,
    toCamelCase,
    changeCase,
    genUUID,
    isString,
    toggleCase,
}
